/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import bropsdox.Bropsdox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import client.Client;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import util.Utilities;

/**
 *
 * @author ACRCOM
 */
public class desktopUI extends JFrame implements ActionListener{
    public JLayeredPane panel;
    ImageIcon background, logo;
    public JLabel lbackground, lusername, lpassword, llogo, lerror;
    JTextField tusername;
    JPasswordField tpassword;
    JButton login;
    public Client client;
    public DefaultTableModel model;
    public boolean SynAvailable = false;
    public Thread synchroThread = new Thread() {
        @Override
        public void run() {
            long lastSync = System.currentTimeMillis();
            while (SynAvailable) {
                if (System.currentTimeMillis() - lastSync > Utilities.SYNC_PERIOD) {
                    client.synchronize();
                    lastSync = System.currentTimeMillis();
                }
            }
        }
    };
    
    private static desktopUI Singleton = new desktopUI();

    public static desktopUI getSingleton(){
      if (Singleton == null){
        Singleton = new desktopUI();
      }
      return Singleton;
    }
    
    public desktopUI(){
        super("BropsDox");
        setSize(640,480);
        
        panel = new JLayeredPane();
        panel.setBounds(0,-17,640,480);

        getContentPane().setLayout(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        background = new ImageIcon(this.getClass().getResource("/gui/bg.png"));
        
        lbackground = new JLabel(background);
        lbackground.setBounds(0, 0, 640, 480);
        panel.add(lbackground, new Integer(1));
        
        logo = new ImageIcon(this.getClass().getResource("/gui/logo.png"));
        
        llogo = new JLabel(logo);
        llogo.setSize(llogo.getPreferredSize());
        llogo.setLocation(350,100);        
        
        lusername = new JLabel("User Name");
        lusername.setSize(lusername.getPreferredSize());
        lusername.setLocation(350, 225);

        lpassword = new JLabel("Password");
        lpassword.setSize(lpassword.getPreferredSize());
        lpassword.setLocation(350, 275);
        
        tusername = new JTextField(20);
        tusername.setLocation(450, 225);
        tusername.setSize(150, 25);

        tpassword = new JPasswordField(20);
        tpassword.setLocation(450, 275);
        tpassword.setSize(150, 25);
        
        login = new JButton("Login");
        login.setSize(login.getPreferredSize());
        login.setLocation(525,325);
        login.addActionListener(this);
        
        model = new DefaultTableModel(new Object [][] {},
            new String [] {
                "Name", "Path"
            });
        File f = new File("files.txt");
        if(f.exists()){
            try {
                Scanner Sc = new Scanner(f);
                while (Sc.hasNextLine()) {
                   String nm = Sc.nextLine();
                   String dr = Sc.nextLine();
                   model.addRow(new Object[]{nm,dr});
                }
                Sc.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(desktopUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                f.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(Bropsdox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        client = new Client(model);
        
        panel.add(llogo, new Integer(2));
        panel.add(lusername, new Integer(2));
        panel.add(lpassword, new Integer(2));
        panel.add(tusername, new Integer(2));
        panel.add(tpassword, new Integer(2));
        panel.add(login, new Integer(2));
        
        getContentPane().add(panel);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == login){
            if (client.isLoginSuccess(tusername.getText(), tpassword.getText())){
                dashboard.getSingleton();
                synchroThread.start();
                SynAvailable = true;
                this.dispose();
            } else {
                lerror = new JLabel("Login Failed.");
                lerror.setSize(lerror.getPreferredSize());
                lerror.setLocation(350, 325);
                panel.add(lerror, new Integer(2));
            }
        }
    }
    
    public static void main(String[] args){
        desktopUI.getSingleton();
    }
}
